package com.hibernate.hrm.util;
import com.hibernate.hrm.entity.Contract;
import com.hibernate.hrm.entity.Department;
import com.hibernate.hrm.entity.Position;
import com.hibernate.hrm.entity.User;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ExcelExportUtil {

    public static void exportEmployeesToExcel(List<User> employees, HttpServletResponse response) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("员工列表");

        // 创建标题行样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        String[] headers = {"员工ID", "姓名", "性别", "年龄", "部门", "职位", "入职日期", "邮箱", "电话", "状态"};

        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }

        // 填充数据
        int rowNum = 1;
        for (User employee : employees) {
            Row row = sheet.createRow(rowNum++);

            row.createCell(0).setCellValue(employee.getUserId());
            row.createCell(1).setCellValue(employee.getRealName());
            row.createCell(2).setCellValue(employee.getGender().toString());
            row.createCell(3).setCellValue(employee.getAge());
            row.createCell(4).setCellValue(employee.getDepartmentID().getDepartmentName());
            row.createCell(5).setCellValue(employee.getJobPositionID().getPositionName());
            row.createCell(6).setCellValue(employee.getStartingDate().toString());
            row.createCell(7).setCellValue(employee.getEmail());
            row.createCell(8).setCellValue(employee.getPhone());
            row.createCell(9).setCellValue(employee.getStatus() ? "在职" : "离职");
        }

        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=employees.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    public static void exportDepartmentsToExcel(List<Department> departments, HttpServletResponse response) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("部门列表");

        // 创建标题行样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        String[] headers = {"部门ID", "部门名称", "部门编码", "部门级别", "父部门", "负责人", "状态", "创建时间"};

        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }

        // 填充数据
        int rowNum = 1;
        for (Department department : departments) {
            Row row = sheet.createRow(rowNum++);

            row.createCell(0).setCellValue(department.getDepartmentID());
            row.createCell(1).setCellValue(department.getDepartmentName());
            row.createCell(2).setCellValue(department.getDepartmentCode());
            row.createCell(3).setCellValue(department.getDepartmentLevel());

            // 处理父部门名称
            String parentName = department.getDepartmentParentID() != null ?
                    department.getDepartmentParentID().getDepartmentName() : "无";
            row.createCell(4).setCellValue(parentName);

            // 处理负责人名称
            String headName = department.getDepartmentHeadID() != null ?
                    department.getDepartmentHeadID().getRealName() : "未指定";
            row.createCell(5).setCellValue(headName);

            row.createCell(6).setCellValue(department.getStatus() ? "启用" : "禁用");
            row.createCell(7).setCellValue(department.getCreateTime().toString());
        }

        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=departments.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    public static void exportPositionsToExcel(List<Position> positions, HttpServletResponse response) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("职位列表");

        // 创建标题行样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        String[] headers = {"职位ID", "职位名称", "职位编码", "所属部门", "职级", "最大人数", "基本薪资", "津贴", "年假天数", "状态", "创建时间"};

        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }

        // 填充数据
        int rowNum = 1;
        for (Position position : positions) {
            Row row = sheet.createRow(rowNum++);

            row.createCell(0).setCellValue(position.getPositionId());
            row.createCell(1).setCellValue(position.getPositionName());
            row.createCell(2).setCellValue(position.getPositionCode());
            row.createCell(3).setCellValue(position.getDepartmentID().getDepartmentName());
            row.createCell(4).setCellValue(position.getPositionRoleID().getRoleName());
            row.createCell(5).setCellValue(position.getMaxHeadCount() != null ? position.getMaxHeadCount() : 0);
            row.createCell(6).setCellValue(position.getBaseSalary() != null ? position.getBaseSalary().doubleValue() : 0);
            row.createCell(7).setCellValue(position.getAllowance() != null ? position.getAllowance().doubleValue() : 0);
            row.createCell(8).setCellValue(position.getAnnualLeaveDays() != null ? position.getAnnualLeaveDays().doubleValue() : 0);
            row.createCell(9).setCellValue(position.getStatus() ? "启用" : "禁用");
            row.createCell(10).setCellValue(position.getCreateTime().toString());
        }

        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=positions.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    public static void exportContractsToExcel(List<Contract> contracts, HttpServletResponse response) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("合同列表");

        // 创建标题行样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        String[] headers = {
                "合同ID", "合同编号", "合同名称", "合同类型",
                "甲方签署人", "乙方签署人", "生效日期", "到期日期",
                "状态", "所属部门", "创建人", "是否可转让",
                "违约金率", "创建时间", "更新时间"
        };

        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }

        // 填充数据
        int rowNum = 1;
        for (Contract contract : contracts) {
            Row row = sheet.createRow(rowNum++);

            row.createCell(0).setCellValue(contract.getContractId());
            row.createCell(1).setCellValue(contract.getContractNumber());
            row.createCell(2).setCellValue(contract.getContractName());
            row.createCell(3).setCellValue(contract.getContractType().toString());
            row.createCell(4).setCellValue(contract.getSigningPartyA().getRealName());
            row.createCell(5).setCellValue(contract.getSigningPartyB().getRealName());
            row.createCell(6).setCellValue(contract.getEffectiveDate().toString());
            row.createCell(7).setCellValue(contract.getExpiryDate().toString());
            row.createCell(8).setCellValue(contract.getStatus().toString());
            row.createCell(9).setCellValue(contract.getDepartment().getDepartmentName());
            row.createCell(10).setCellValue(contract.getCreator().getRealName());
            row.createCell(11).setCellValue(contract.getIsTransferable() ? "是" : "否");
            row.createCell(12).setCellValue(contract.getPenaltyRate().toString());
            row.createCell(13).setCellValue(contract.getCreateTime().toString());
            if (contract.getUpdateTime() != null) {
                row.createCell(14).setCellValue(contract.getUpdateTime().toString());
            } else {
                row.createCell(14).setCellValue("");
            }
        }

        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=contracts.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
